<template>
  <div class="search">
    <nav-bar class="navbar">
      <div slot="center">搜索</div>
    </nav-bar>

    <form class="search_form" action="#">
      <input
        type="search"
        name="search"
        placeholder="请输入商家或美食名称"
        class="search_input"
        v-model.trim="keyword"
      />
      <input type="submit" name="submit" class="search_submit" @click.prevent="searchShops" />
    </form>

    <section class="list" v-if="!noSearchShops">
      <ul class="list_container">
        <!--:to="'/shop?id='+item.id"-->
        <router-link
          :to="{path:'/shop', query:{id:item.id}}"
          tag="li"
          v-for="item in shops"
          :key="item.id"
          class="list_li"
        >
          <section class="item_left">
            <img :src="'http://cangdu.org:8001/img/' + item.image_path" class="restaurant_img" />
          </section>
          <section class="item_right">
            <div class="item_right_text">
              <p>
                <span>{{item.name}}</span>
              </p>
              <p>月售 {{item.month_sales||item.recent_order_num}} 单</p>
              <p>{{item.delivery_fee||item.float_minimum_order_amount}} 元起送 / 距离{{item.distance}}</p>
            </div>
          </section>
        </router-link>
      </ul>
    </section>

    <div class="search_none" v-else>很抱歉！无搜索结果</div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import NavBar from "../../components/common/navbar/NavBar";
export default {
  data() {
    return {
      // 搜索的内容
      keyword: "",
      noSearchShops: false
    };
  },
  methods: {
    searchShops() {
      // 有所有的内容才发送请求，如果没有就不发送请求
      if (this.keyword) {
        // 发送请求获取店铺信息
        this.$store.dispatch("getSearchShopInfo", this.keyword);
      }
    }
  },
  computed: {
    ...mapState({
      shops: state => state.search.searchShopList
    })
  },

  components: {
    NavBar
  },
  watch: {
    shops(value){
      if(!value.length) { // 没有数据
          this.noSearchShops = true
        } else {// 有数据
          this.noSearchShops = false
        }
    }
  },
};
</script>
<style lang='less' scoped>
.search {
  .navbar {
    background-color: #02a774;
    color: #fff;
    position: fixed;
    font-size: 22px;
  }

  .search_form {
    *zoom: 1;
    margin-top: 45px;
    background-color: #fff;
    padding: 12px 8px;

    &::after {
      content: "";
      display: block;
      clear: both;
    }

    input {
      height: 35px;
      padding: 0 4px;
      border-radius: 2px;
      font-weight: bold;
      outline: none;

      &.search_input {
        float: left;
        width: 79%;
        border: 4px solid #f2f2f2;
        font-size: 14px;
        color: #333;
        background-color: #f2f2f2;
      }

      &.search_submit {
        float: right;
        width: 18%;
        border: 4px solid #02a774;
        border-radius: 10px;
        font-size: 16px;
        color: #fff;
        background-color: #02a774;
      }
    }
  }

  .list {
    .list_container {
      background-color: #fff;
      .list_li {
        display: flex;
        justify-content: center;
        padding: 10px;
        border-bottom: 1px solid #e4e4e4;
        .item_left {
          margin-right: 10px;
          .restaurant_img {
            width: 50px;
            height: 50px;
            display: block;
          }
        }
        .item_right {
          font-size: 12px;
          flex: 1;
          .item_right_text {
            p {
              line-height: 12px;
              margin-bottom: 6px;
              &:last-child {
                margin-bottom: 0;
              }
            }
          }
        }
      }
    }
  }
  .search_none {
    margin: 0 auto;
    color: #333;
    background-color: #fff;
    text-align: center;
    margin-top: 0.125rem;
  }
}
</style>